package net.rlair;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Hello world!
 *
 */
public class App {
    private static final Pattern p = Pattern.compile("(\"|\')[A-Za-z0-9\\-\\(\\)（）？！\\!\\?\\:、；]*[\\u4e00-\\u9fa5]+[A-Za-z0-9\\-\\(\\)（）？！\\!\\?\\:、；\\u3002\\uff1b\\uff0c\\uff1a\\u201c\\u201d\\uff08\\uff09\\u3001\\uff1f\\u300a\\u300b||\\,\\:\\; \\u4e00-\\u9fa5]*(\"|\')");


    private static final Pattern quchu = Pattern.compile("[\\s|\t|\r|\n]");

    private static final Map<String, String> result = new TreeMap<>();
    public static void main( String[] args )
    {
        //读取测试
        readString("D:\\workspace\\rloa\\oa-web\\src\\main\\webapp\\WEB-INF\\api");
        //D:\\ChineseOA\\web.text
        writeStringToFile("D:\\workspace\\rloa\\api-ftl.txt");
        //resdString("D:\\test-i18");
        //getAllFile("D:\\rlair-work\\rloa\\oa-web\\src\\main\\java\\net\\rlair\\oa");
    }


    public static void readString(String fileDir) {
        List<File> fileList = new ArrayList<>();
        File file = new File(fileDir);
        if(file.getAbsolutePath().contains("lib") || file.getAbsolutePath().contains("build") ){
            return;
        }
        if (file.isFile()) {
            fileList.add(file);
        } else {
            //D:\rlair-work\rloa\oa-web\src\main\webapp\js\lib
            //System.out.println(file.getAbsolutePath());
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
            for (File f : files) {
                if (f.isFile()) {
                    fileList.add(f);
                } else if (f.isDirectory()) {
                    readString(f.getAbsolutePath());
                }
            }
        }
        for (File f1 : fileList) {
            final String name = f1.getName().substring(0, f1.getName().lastIndexOf("."))
                    + "_" + UUID.randomUUID().toString().substring(0, 4); //避免重名
            try (InputStreamReader read = new InputStreamReader(new FileInputStream(f1), "UTF-8")) {
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                StringBuilder sb = new StringBuilder();
                int i = 0;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    if (lineTxt == null) {
                        continue;
                    }
                    lineTxt.trim();
                    Matcher ma = quchu.matcher(lineTxt);
                    lineTxt = ma.replaceAll("");
                    //System.out.println(lineTxt);
                    if (lineTxt.startsWith("//") || lineTxt.startsWith("/*")
                            || lineTxt.startsWith("@LogOpera") || lineTxt.startsWith("*")
                            || lineTxt.startsWith("*@a") || lineTxt.startsWith("@Export")){
                        continue;
                    }
                    //提取中文
                    final Matcher m = p.matcher(lineTxt);
                    while (m.find()) {
                        //System.out.println(m.group());
                        //result.put(m.group(), name + "_" + (i++));
                        Collection<String> values = result.values();
                        String group = m.group().substring(1, m.group().length()-1);
                        if (!values.contains(group)) {
                            result.put(name + "_" + (i++), group);
                        }
                    }
                    sb.append(lineTxt);
                }
                //提取
            } catch (Exception ex) {
                Logger.getLogger(Replace.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    public static void writeStringToFile(String filePath) {
        try {
            File file = new File(filePath);
            PrintStream ps = new PrintStream(new FileOutputStream(file));
            for (final String key: result.keySet()) {
                //System.out.println(result.get(key)+ "=" +key);
                //ps.println(result.get(key)+ "=" +key);
                System.out.println(key+ "=" + result.get(key));
                ps.println(key+ "=" + result.get(key));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void getAllFile(String filePath) {
        List<File> fileList = new ArrayList<>();
        File file = new File(filePath);
        if (file.isFile()) {
            System.out.println(file.getName());
        } else {
            System.out.println(file.getAbsolutePath());
            if(file.getAbsolutePath().contains("contacts")){
                System.err.println(file.getAbsolutePath());
                return;
            }
            File[] files = file.listFiles();
            if (files == null) {
                return;
            }
            for (File f : files) {
                if (f.isFile()) {
                    fileList.add(f);
                } else if (f.isDirectory()) {
                    readString(f.getAbsolutePath());
                }
            }
        }
    }
}
